package tech.xujian.lock.distributed.lockdistributed.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import tech.xujian.lock.distributed.lockdistributed.service.RedisService;

import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Override
    public void set(String k, String value) {
        redisTemplate.opsForValue().set(k,value);
    }

    @Override
    public void set(String k, String value, int expireMinute) {
        redisTemplate.opsForValue().set(k,value,expireMinute, TimeUnit.MINUTES);
    }

    @Override
    public String get(String k) {
        return redisTemplate.opsForValue().get(k);
    }

    @Override
    public long getLong(String k) {
        String str = get(k);
        return str == null ? 0 : Long.parseLong(str);
    }

    @Override
    public long increment(String k) {
        return redisTemplate.opsForValue().increment(k);
    }

    @Override
    public String getAndDelete(String k) {
        return redisTemplate.opsForValue().getAndDelete(k);
    }

    @Override
    public long decrement(String k) {
        return redisTemplate.opsForValue().decrement(k);
    }
}
